package proj1.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import proj1.dao.Model;
import proj1.dao.Radiator;
import proj1.dao.Radiator.SIZE;
import proj1.dao.Vendor;
import proj1.util.Logger;

public class EditRadiator extends JPanel {

	private static Logger log = Logger.getLogger(RadiatorDescription.class);
	private static final long serialVersionUID = 1L;

	JTable radiatorTable;
	Radiator rad;
	
	public EditRadiator (final ApplicationFrame mainFrame, final Model model, final SIZE size, final Vendor vendor, Radiator r){
		rad = r;
		JPanel panel = new JPanel();
		JButton save = new JButton("Save");
		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
		final String[][] data = {{"Part Number", r.getPartNumber()},
				{"Core", r.getCore()},
				{"INHEAD",r.getInHead()},
				{"OUTHEAD",r.getOutHead()},
				{"INCON",r.getInCon()},
				{"OUTCON",r.getOutCon()},
				{"TMOUNT",r.getTMount()},
				{"OILCOOL",r.getOilCool()},
				{"PRICE","$"+r.getPrice().toString()},
				{"AMOUNT",r.getQuantity().toString()}};
		String[] headers = {"Property", "Value"};
		radiatorTable = new JTable(data, headers){
	        private static final long serialVersionUID = 1L;
	        public boolean isCellEditable(int row, int column) {                
	                return true;
	        };
		};
		radiatorTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		JScrollPane jsp = new JScrollPane(radiatorTable);
		Dimension d = new Dimension((int) jsp.getPreferredSize().getWidth(), 
									(radiatorTable.getRowHeight()+1)*11);
		jsp.setPreferredSize(d);
		panel.add(jsp);
		panel.add(save);
		add(panel, BorderLayout.CENTER);
		setVisible(true);
		
		save.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {		
				if (radiatorTable.isEditing()) { 
					radiatorTable.getCellEditor().stopCellEditing();  
				}
				
				mainFrame.setMessage("Radiator Has Been Edited");
				String newPartNumber = data[0][1];
				String newCore = data[1][1];
				String newInHead = data[2][1];
				String newOutHead = data[3][1];
				String newInCon = data[4][1];
				String newOutCon = data[5][1];
				String newTMount = data[6][1];
				String newOilCool = data[7][1];
				Double newPrice = new Double(data[8][1].substring(1));// remove the $ sign
				Long newQuantity = new Long(data[9][1]);
				rad.update(model, size, vendor, newPartNumber, newCore, newInHead, newOutHead, newInCon, newOutCon, newTMount, newOilCool, newPrice, newQuantity);
				
			}
		});
	}
	public String toString(){
		return "Edit Radiator Info";
	}
}
